require('./index.scss');
var Tool = require('common/tool');
var subMenuList = require('../subMenuList');
require('comp/tab');
/* 密码验证规则 */
avalon.validators.pwd = {
    message: '密码需为6-16位字母与数字的组合',
    get: function (value, field, next) {
        //想知道它们三个参数是什么,可以console.log(value, field,next)
        var reg = /(?!\d+$)(?![a-zA-Z]+$)^[0-9a-zA-Z]{6,16}$/;
        var ok = reg.test(value);
        next(ok)
        return value
    }
}
avalon.validators.pwdAgain = {
    message: '新密码输入不一致',
    get: function (value, field, next) {
        // console.log('pwdAgain',arguments,vm.form.newPwd);
        next(value==vm.form.newPwd)
        return value
    }
}
var vm = avalon.define({
    $id: 'updatePwd',
    tabList:{
        active:4,
        list:subMenuList
    },
    account:'',
    eyeActive: false,
    form:{
        originalPwd:'',
        originalPwdError:'',
        newPwd:'',
        newPwdError:'',
        newPwdToo:'',
        newPwdTooError:'',
    },
    loading:false,
    resetForm:function(){
        this.form.originalPwd='';
        this.form.originalPwdError='';
        this.form.newPwd='';
        this.form.newPwdError='';
        this.form.newPwdToo='';
        this.form.newPwdTooError='';
        return false;
    },
    switchEye:function(){
        this.eyeActive = !this.eyeActive;
        $("#originalPwd")[0].type = this.eyeActive ? 'text':'password';
    },
    triggerValida:function(){
        vm.validate.onManual();
        return false;
    },
    validate: {
        //禁止提交时自动验证
        validateAllInSubmit: false,
        onManual:avalon.noop,//IE6-8必须指定,avalon一会儿会重写这方法
        /* 整体验证后触发 */
        onValidateAll: function (reasons) {
            console.log('onValidateAll',reasons);
            if( reasons.length>0 ) {
                reasons.forEach(function(error,index){
                    var _id = reasons[index].element.id;
                    vm.form[_id+"Error"] = error.message;//打印所有错误
                });
                return false;
            }
            updatePwdAjax();
        },
        onError:function(reasons){
            if(!reasons || reasons.length===0) return false;
            var _id = reasons[0].element.id;
            var a = reasons.map(function (el) {
                return  el.getMessage()
            });
            vm.form[_id+"Error"] = a.join(', ');//打印所有错误
        },
        onSuccess:function(reasons){
            var _id = reasons[0].element.id;
            vm.form[_id+"Error"] = '';
        }
    }
    
});

vm.$watch('onReady',function(){
    var loop = setInterval(function(){
        if(rootVm.user.account){
            vm.account = rootVm.user.account;
            clearInterval(loop);
        }
    },200);
});


/* 修改邮箱地址 */
function updatePwdAjax(){
    vm.loading = true;
    var form = vm.form;
    Tool.ajax({
        url: '/updatePassWord',
        data:{
            password: form.originalPwd,
            password1: form.newPwd,
            password2: form.newPwdToo,
        },
        type:"post"
    }).done(function(res){
        vm.loading = false;

        layer.alert('修改成功！', function(){
            location.reload();
        });
    });
}